package com.weiyin.examination.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.google.gson.Gson;
import com.weiyin.examination.R;
import com.weiyin.examination.application.Constant;
import com.weiyin.examination.http.DataService;
import com.weiyin.examination.http.HttpRequestListener;
import com.weiyin.examination.http.RequestUrl;
import com.weiyin.examination.http.RetrofitClient;
import com.weiyin.examination.module.UserModule;
import com.weiyin.examination.util.LayoutHelper;
import com.weiyin.examination.util.LogUtil;
import com.weiyin.examination.util.SPUtil;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.ResponseBody;

/**
 * 启动界面
 *
 * @author njf
 */
public class StartActivity extends BaseActivity implements HttpRequestListener<ResponseBody>, View.OnClickListener {
    private static final String TAG = StartActivity.class.getSimpleName();
    private RelativeLayout startActivityLayout;
    private Bitmap bitmap;

    private Handler mHandler = new Handler();

    private EditText startTickNumber, startName;

    private DataService dataService;
    private UserModule userModule;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        LayoutHelper layoutHelper = new LayoutHelper(this);
        layoutHelper.scaleView(findViewById(R.id.startActivityLayout));
        initView();
    }

    public void initView() {
        dataService = new DataService(RetrofitClient.getInstance(this).getService());
        startActivityLayout = (RelativeLayout) findViewById(R.id.startActivityLayout);
        bitmap = Constant.readBitMap(this, R.drawable.login_background);
        BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
        startActivityLayout.setBackground(bitmapDrawable);
        startName = (EditText) findViewById(R.id.startName);
        Button startSubmit = (Button) findViewById(R.id.startSubmit);
        startTickNumber = (EditText) findViewById(R.id.startTickNumber);

        startSubmit.setOnClickListener(this);
    }

    private void submit(String tickNumber, String name) {
        dataService.login(tickNumber, name, this);
    }

    @Override
    public void onCompleted(String url) {

    }

    @Override
    public void onError(Throwable e, String url) {

    }

    @Override
    public void onNext(ResponseBody responseBean, String url) {
        switch (url) {
            case RequestUrl.LOGIN:
                String json = null;
                try {
                    json = responseBean.string();
                    LogUtil.i(TAG, json);
                    JSONObject jsonObject = new JSONObject(json);
                    if (jsonObject.getInt(Constant.RESP_CODE) == 0) {
                        Gson gson = new Gson();
                        userModule = gson.fromJson(jsonObject.getJSONObject("info").toString(), UserModule.class);

                        Intent intent = new Intent();
                        intent.putExtra("UserModule", userModule);
                        SPUtil.setSharedPreferencesData(this, "level", userModule.getLevel());
                        SPUtil.setSharedPreferencesData(this, "number", userModule.getNumber());
                        SPUtil.setSharedPreferencesData(this, "paperType", userModule.getPaperType());
                        SPUtil.setSharedPreferencesData(this, "name", userModule.getName());
                        intent.setClass(StartActivity.this, PersonActivity.class);
                        startActivity(intent);
                    } else {
                        showToast(jsonObject.getString(Constant.RESP_INFO));
                    }
                } catch (IOException | JSONException e) {
                    e.printStackTrace();
                }

                break;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.startSubmit:
                String tickNumber = startTickNumber.getText().toString();
                String name = startName.getText().toString();

                if (StringUtils.isEmpty(tickNumber) && StringUtils.isEmpty(name)) {
                    showToast(getResources().getString(R.string.startTextNull));
                    return;
                }
                submit(tickNumber, name);

                break;
        }
    }
}
